﻿using System.Linq;
using System.Collections.Generic;
using INHollandair.Data;

namespace INHollandair.Core.Repositories
{
    public class ProductRepository
    {
        //Database object
        private readonly PresentatieDBDataContext _context = new PresentatieDBDataContext();

        public IQueryable<Models.Product> ProductRepo
        {
            get
            {
                return _context.Products.Select(x => new Models.Product
                {
                    Id = x.Id,
                    Name = x.Name,
                    Description = x.Description,
                    Price = x.Price,
                    Image = x.Image,
                    Date = x.Date
                });
            }
        }

        public void InsertProduct(Models.Product product)
        {
            Product productToInsert = new Product
            {
                Name = product.Name,
                Description = product.Description,
                Price = product.Price,
                Image = product.Image,
                Date = product.Date
            };

            _context.Products.InsertOnSubmit(productToInsert);
            _context.SubmitChanges();
        }
        public void UpdateProduct(Models.Product product)
        {
            Product productToUpdate = _context.Products.First(x => x.Id.Equals(product.Id));
            productToUpdate.Name = product.Name;
            productToUpdate.Description = product.Description;
            productToUpdate.Price = product.Price;
            productToUpdate.Image = product.Image;
            productToUpdate.Date = product.Date;

            _context.SubmitChanges();
        }
        public void RemoveProduct(Models.Product product)
        {
            Product productToRemove = _context.Products.First(x => x.Id.Equals(product.Id));
            _context.Products.DeleteOnSubmit(productToRemove);
            _context.SubmitChanges();
        }

        public void InsertProducts(List<Models.Product> products)
        {
            List<Product> productsToInsert = products.Select(
                product => new Product
                {
                    Name = product.Name,
                    Description = product.Description,
                    Price = product.Price,
                    Image = product.Image,
                    Date = product.Date
                }).ToList();

            _context.Products.InsertAllOnSubmit(productsToInsert);
            _context.SubmitChanges();
        }
        public void UpdateProducts(List<Models.Product> products)
        {
            foreach (Models.Product product in products)
            {
                Product productToUpdate = _context.Products.First(x => x.Id.Equals(product.Id));
                productToUpdate.Name = product.Name;
                productToUpdate.Description = product.Description;
                productToUpdate.Price = product.Price;
                productToUpdate.Image = product.Image;
                productToUpdate.Date = product.Date;
            }
            _context.SubmitChanges();
        }
        public void RemoveProducts(List<Models.Product> products)
        {
            List<Product> productsToRemove = products.Select(y => _context.Products.First(x => x.Id.Equals(y.Id))).ToList();
            _context.Products.DeleteAllOnSubmit(productsToRemove);
            _context.SubmitChanges();
        }
    }
}
